package com.lfg.leetcode.leetcode0;


public class No43Multiply {

    public static void main(String[] args) {
        String ret = multiply("99", "99");
        System.out.println(ret);
    }

    public static String multiply(String num1, String num2) {
        int length1 = num1.length();
        int length2 = num2.length();
        int[] ret = new int[length1 + length2];
        int pos = 1;
        for (int i = num1.length() - 1; i >= 0; i--) {
            int a = num1.charAt(i) - '0';
            int length = ret.length - pos;
            pos++;
            int total = 0;
            for (int j = num2.length() - 1; j >= 0; j--) {
                int b = num2.charAt(j) - '0';
                int result = a * b;
                total += ret[length] + result % 10;
                ret[length] = total % 10;
                total = result / 10 + total / 10;
                length--;
            }
            while (total > 0) {
                total += ret[length];
                ret[length] = total % 10;
                total = total / 10;
                length--;
            }
        }
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (; i < ret.length; i++) {
            if (ret[i] == 0) {
                continue;
            }
            break;
        }
        for (; i < ret.length; i++) {
            sb.append(ret[i]);
        }
        if (sb.length() == 0) {
            return "0";
        }
        return sb.toString();
    }
}

